function Rand() {
    return Math.floor(Math.random() * 10000);
}
var MBot = new Vue({
    el: '#app',
    data: function () {
        return {
            qa: [
                { "q": "我爱你$1", "a": "我也爱你" },
                {"q":"YEMASTER是$2", "a":"就是我啦|当然是可爱的我"}
            ],
            defaultAnswer: "。。。|emmmm",
            botName: "Yemaster",
            smit: "发送",
            mainchat: "",
            lastStamp: 0,
            version: "V2.0.1",
            programName: "MBot",
            updateLogs: [
                "采用vue.js重构项目，代码更易懂", "如果你想更新词库，请阅读下面的说明"
            ],
            writeReadme: "首先你得打开GitHub项目地址：<a href=\"https://github.com/mstcomp/mstcomp.github.io/blob/master/main.js\">点我</a><br>点击右上角编辑，然后开始编辑。<br>找到data中qa数组，你可以添加<br>添加词条你可以按照上面的示例新加一个<code>{\"q\": ***, \"a\": ***}</code><br>注意确保逗号正确性。<br>各个语句用|分开，每个问题后面必须加上$和数字1~3<br>$1表示将问题逐字拆分后查找<br>$2表示完整查找<br>$3表示用match查找<br>其中defaultAnswer表示default回答<br>P.S.如果你想修改的话最好看懂源码"
        }
    },
    computed: {
        getTime: function () {
            var obj = new Date();
            return obj.getFullYear() + "-" + (obj.getMonth() + 1) + "-" + obj.getDate() + " " + obj.getHours() + ":" + obj.getMinutes() + ":" + obj.getSeconds();
        },
        getTimeStamp: function () {
            return (new Date()).valueOf();
        },
        keyWords: function () {
            /* 0,1,3,6,10,15,21,28,36,45,55,66,78,91 */
            var Skey = "Yelm*sa9kss4hs*t(2msje3ms92mr8JsmeN& MSK*23$jLda9M,:lsoo#(SANSF*&Tv&*@*#@H$*H#e$#@*$H*&#G*GsSHs *(SY*&YN* T&@!%RD*NA(DMUSMIZYBYWNQUIQMOISMOAKAKASKAGZXM*SH";
            var fm = 0;
            var rT = '';
            for (var i = 0, j = 1; i < Skey.length; i += j, ++j) {
                rT += Skey[i];
            }
            return rT;
        }
    },
    methods: {
        fd: function (a, b) {
            a = a.toUpperCase();
            c = b.split("");
            for (var i = 0; i < c.length; i++) {
                if (a.indexOf(c[i]) == -1)
                    return false;
            }
            return true;
        },
        fd2: function (a, b) {
            a = a.toUpperCase();
            if (a.indexOf(b) == -1)
                return false;
            return true;
        },
        fd3: function (a, b) {
            a = a.toUpperCase();
            if (a.match(b)) {
                return true;
            }
            return false;
        },
        toDown: function () {
            var divd = document.getElementById('mainpan');
            divd.scrollTop = divd.scrollHeight;
        },
        showLove: function () {
            app.style.opacity = '0';
            setTimeout(function () { app.style.display = "none"; }, 2000);
            document.body.style.backgroundColor = "pink";
            var allHeight = document.body.clientHeight - 100;
            var allWidth = document.body.clientWidth - 110;
            var heartNum = Rand() % 300 + 300;
            var nCount = 0, nCount2 = 0;
            document.getElementById('heartBoard').style.display = '';
            for (var i = 1; i <= heartNum; ++i) {
                document.getElementById('heartBoard').innerHTML = document.getElementById('heartBoard').innerHTML + "<div id='heartBox" + i + "' style='float:left;position:absolute;top:" + Rand() % allHeight + "px;left:" + Rand() % allWidth + "px;'><div id='heart" + i + "' class='heart'></div></div>";
            }
            setTimeout(function () {
                var heartSe = setInterval(function () {
                    ++nCount;
                    if (nCount > heartNum)
                        clearInterval(heartSe);
                    document.getElementById("heart" + nCount).style.opacity = nCount / heartNum;
                }, 30);
            }, 2000);
            setTimeout(function () { document.getElementById("gggyyy").style.top = "20%"; }, 12000);
            setTimeout(function () { document.getElementById("heartBoard").style.filter = "blur(12px)"; }, 14000);
        },
        botSay: function (e) {
            if (this.getTimeStamp - this.lastStamp > 10000)
                this.mainchat += "<span style=\"display: block; text-align: center\">" + this.getTime + "</span>";
            else this.mainchat += "<br>";
            this.lastStamp = this.getTimeStamp;
            this.mainchat += "<div style=\"width:100%;float:none;display:block\"><a><img src=\"http://q.qlogo.cn/headimg_dl?dst_uin=1440169768&spec=160\" width=32 height=32 style=\"border:1px solid #000;border-radius:50%;float:left\"></a><div class=\"item-left\"><p style=\"margin:6px\">" + e + "</p></div></div><div style=\"clear:both;\"></div>";
            this.toDown();
        },
        wantSupport: function () {
            alert("爱你哟");
        },
        youSay: function () {
            var texTT = mainsay.value;
            if (texTT == "")
                return;
            mainsay.value = '';
            if (this.getTimeStamp - this.lastStamp > 10000)
                this.mainchat += "<span style=\"display: block; text-align: center\">" + this.getTime + "</span>";
            else this.mainchat += "<br>";
            this.lastStamp = this.getTimeStamp;
            this.mainchat += "<div style=\"width:100%;float:none;display:block\"><a><img src=\"http://q.qlogo.cn/headimg_dl?dst_uin=1440169768&spec=160\" width=32 height=32 style=\"border:1px solid #000;border-radius:50%;float:right\"></a><div class=\"item-right\"><p style=\"margin:6px\">" + texTT + "</p></div></div><div style=\"clear:both;\"></div>";
            var rSay = "";
            bbq:
            for (var i = 0; i < this.qa.length; ++i) {
                var qs = this.qa[i].q.split('|');
                for (var j = 0; j < qs.length; ++j) {
                    var ques = qs[j].split('$')[0];
                    var mod = qs[j].split('$')[1];
                    if (mod == 1) {
                        if (this.fd(texTT, ques)) {
                            var mq = this.qa[i].a.split('|');
                            rSay = mq[Rand() % mq.length];
                            break bbq;
                        }
                    }
                    else if (mod == 2) {
                        if (this.fd2(texTT, ques)) {
                            var mq = this.qa[i].a.split('|');
                            rSay = mq[Rand() % mq.length];
                            break bbq;
                        }
                    }
                    else if (mod == 3) {
                        if (this.fd3(texTT, ques)) {
                            var mq = this.qa[i].a.split('|');
                            rSay = mq[Rand() % mq.length];
                            break bbq;
                        }
                    }
                }
            }
            if (texTT.split(':')[0] == 'calc') {
                try {
                    rSay = "运行成功！<br>结果:" + eval(texTT.split(":")[1]);
                }
                catch (exception) {
                    rSay = "运行失败！<br>结果:" + exception;
                }
            }
            if (this.fd(texTT, "你是") && !this.fd(texTT, "吗")) {
                rSay = "我是" + this.botName + "哦";
            }
            if (texTT.match(/^[a-z,A-Z,\s]+$/)) {
                rSay = "我不懂英文的哦";
            }
            if (texTT.match(/^[0-9]+$/)) {
                rSay = parseInt(texTT) + 1;
            }
            if (rSay == "") {
                var mq = this.defaultAnswer.split('|');
                rSay = mq[Rand() % mq.length];
            }
            this.botSay(rSay);
            this.toDown();
        }
    }
})
var hiddenProperty = 'hidden' in document ? 'hidden' :
    'webkitHidden' in document ? 'webkitHidden' :
        'mozHidden' in document ? 'mozHidden' :
            null;
var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange');
var onVisibilityChange = function () {
    if (!document[hiddenProperty]) {
        document.title = MBot.botName + "聊天";
    } else {
        document.title = "呜呜呜不理我了";
    }
}
document.addEventListener(visibilityChangeEvent, onVisibilityChange);
document.title = MBot.botName + "聊天";
document.getElementById("KeyWords").innerHTML = MBot.keyWords;
MBot.botSay("您好，我是" + MBot.botName + "，欢迎跟我聊天哦");
